package org.devgframwork.datasync.core.support.executors;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.devgframwork.datasync.core.entity.ScheduleConfig;
import org.devgframwork.datasync.core.entity.TargetConfig;
import org.devgframwork.datasync.core.model.ExecutorModel;
import org.devgframwork.datasync.core.model.ExecutorTypeEnum;
import org.devgframwork.datasync.core.model.ResultModel;
import org.devgframwork.datasync.core.support.DataSourceMemory;
import org.devgframwork.datasync.core.support.executors.source.JdbcMySqlExecutor;
import org.devgframwork.datasync.core.support.service.ScheduleConfigService;
import org.devgframwork.datasync.core.utils.CommonUtil;

import java.util.List;

@Slf4j
public class AbstractProxyExecutor implements AbstractExecutor {

    private AbstractExecutor abstractExecutor;

    private ExecutorModel executorModel;

    public AbstractProxyExecutor(ExecutorModel executorModel) {
        this.executorModel = executorModel;
    }

    public ResultModel invoke() {
        String routingKey = executorModel.getRoutingKey();
        if (CommonUtil.eq(routingKey, CommonUtil.dataSyncAll)) {
            return dataSyncAllExecutor();
        }
        this.abstractExecutor = new TriggerExecutor();
        return invoke(executorModel);
    }

    @Override
    public ResultModel invoke(ExecutorModel executorModel) {
        return abstractExecutor.invoke(executorModel);
    }

    private ResultModel dataSyncAllExecutor() {
        // 消息为taskName
        ScheduleConfigService scheduleConfigService = DataSourceMemory.applicationContext.getBean(ScheduleConfigService.class);
        QueryWrapper<ScheduleConfig> queryWrapper = new QueryWrapper();
        queryWrapper.eq("TASK_NAME", executorModel.getTaskName());
        List<ScheduleConfig> list = scheduleConfigService.list(queryWrapper);
        if (CommonUtil.isEmpty(list)) {
            return ResultModel.newFail("任务列表不能为空");
        }
        ScheduleConfig scheduleConfig = list.get(0);
        String soureType = scheduleConfig.getSourceType();
        String targetInfo = scheduleConfig.getTargetInfo();
        if (CommonUtil.isNullOrEmpty(targetInfo)) {
            log.error("targetInfo cannot be null");
            return ResultModel.newFail("targetInfo不能为空");
        }
        List<TargetConfig> targetConfigList = JSONObject.parseArray(targetInfo, TargetConfig.class);
        if (CommonUtil.isEmpty(targetConfigList)) {
            log.error("targetInfo cannot be null");
            return ResultModel.newFail("targetInfo不能为空");
        }
        scheduleConfig.setTargetConfigList(targetConfigList);
        scheduleConfig.setExecuteName(scheduleConfig.getTaskName());
        this.executorModel.setExt(scheduleConfig);
        if (CommonUtil.eq(soureType, ExecutorTypeEnum.jdbc_mysql.getTypeName())) {
            this.abstractExecutor = new JdbcMySqlExecutor(targetConfigList);
        }
        return this.invoke(executorModel);
    }
}
